angular.module('warehouse-list', ['swipe-list'])

.controller 'WarehouseListCtrl', ($ionicActionSheet,$scope, $state, $http, $ionicHistory, $stateParams, goodsInfo, wfCache,$ionicViewSwitcher,$ionicPopup,userData,paginationFactory,showToast,$ionicScrollDelegate) ->
    paginationSrv = new paginationFactory('WarehouseListCtrl','/updshop/')
    $scope.checkContent=false
    unWatch = paginationSrv.watch (date) ->
        $scope.goodsList = _.map date,(item)->
            goods = {
                id:item.id
                img:item.img
                txt1:item.title
#                txt2:"市场价:￥#{item.marketprice}"
                txt3:"销售价:￥#{item.price}"
            }
        if $scope.checkContent and $scope.goodsList?.length==0
            $scope.nocontent=true
        else
            $scope.nocontent=false
    getShareInfo = (proid)->
        $http.get('/seller/',params:{action:'shareproduct',shopid:userData.getToken(),proid:proid})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                $ionicActionSheet.show {
                    buttons: [
                        {text: '分享到微信朋友圈'}
                        {text: '分享给朋友'}
                    ]
                    titleText:'请选择您想要进行的操作'
                    cancelText: '取消分享'
                    cssClass:'text'
                    cancel:->
                        return false
                    buttonClicked:(index) ->
                        if index == 1
                            Wechat.share({
                                        message: {
                                            title: $scope.shareData.title
                                            description: $scope.shareData.description
                                            thumb: $scope.shareData.shoplogo
                                            media: {
                                                type: Wechat.Type.LINK,
                                                webpageUrl: $scope.shareData.url
                                            }
                                        },
                                        scene: Wechat.Scene.SESSION
                                    },
                                ()->console.log('分享成功~'),
                                (reason) ->console.log(reason)
                            )
                        else
                            Wechat.share({
                                        message: {
                                            title: $scope.shareData.title
                                            description: $scope.shareData.description
                                            thumb: $scope.shareData.shoplogo
                                            media: {
                                                type: Wechat.Type.LINK,
                                                webpageUrl: $scope.shareData.url
                                            }
                                        },
                                        scene: Wechat.Scene.TIMELINE
                                    },
                                ()->console.log('分享成功~'),
                                (reason) ->console.log(reason)
                            )
                }
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '网络异常,请检查网络是否连接'
    _.extend $scope,
        canLoadMore: -> paginationSrv.canLoadMore()

        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'
        goBack: ->
          $ionicViewSwitcher.nextDirection('back')
          $ionicHistory.goBack()
        remove: (i) ->
            console.log 'remove',i
            $http.post('/updshop/',{action:'delproduct',userid:userData.getToken(),id:$scope.goodsList[i].id})
            .success (resp) ->
                if resp?.status == '0'
                    showToast '删除成功'
                    $scope.goodsList.splice(i,1)
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
        batOnLine: () ->
            goodsToUpLoad = []
            ids=''
            _.each $scope.goodsList,(item)->
                if item.isCheckIn == true
                    goodsToUpLoad.push item
                    ids += item.id+','
            ids = ids.slice(0,ids.length-1)
            $http.post("/updshop/",{action:'proshelves',userid:userData.getToken(),id:ids,issale:'1'})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.goodsList = _.difference $scope.goodsList,goodsToUpLoad
                    showToast '上架成功'
                else
                    console.log '失败'
            .error ->
                showToast '您的网络又调皮了'
        pick: (i) ->
            console.log 'pick',i
            goodsInfo.isOnLine = false
            $state.go 'tab.goods-detail', goodId: $scope.goodsList[i].id
        share:(i) ->
            getShareInfo($scope.goodsList[i].id)
    $scope.$on '$destory', ->
        unWatch()
    $scope.$on '$ionicView.enter', ->
        $scope.checkContent=true
        paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'0'
            stype:1
        })
        $ionicScrollDelegate.scrollTop()





